$(function() {
        initHomeContent();
        initEvent();
    }
);

function initEvent() {
    window.onhashchange = function() {
        if (window.location.hash == "#trang-chu") {
            $("#main-board").removeClass("move-left");
        } else {
            getArticleDetails(window.location.hash.substring(1), function(article) {
                $(".details-article").html(article.content);
                $("#origin-url-button").attr("href", article.originUrl);
                var time = article.publicDateString.substring(0, 6);
                var date = article.publicDateString.substring(6);
                $("#details-time").text(time);
                $("#details-date").text(date);
                $("#main-board").addClass("move-left");
            });
        }
    }
};

function initHomeContent() {
    $.ajax({
        type: "GET",
        url: XN_CLOUD_BACK_HOST + "/news/provider/home-page-content.json",
        success: function(xhr) {
            $("#overview-page").html(xhr.homePageContent);
        },
        failure: function(xhr) {
            alert("Load content failure!");
        },
        error: function (xhr, desc, err) {
            alert(desc);
        },
        dataType: "json"
    });
}

function getArticleDetails(symbolicLink, success) {
    var data = {
        symbolicLink: symbolicLink
    };

    $.ajax({
        type: "GET",
        url: XN_CLOUD_BACK_HOST + "/news/provider/article.json",
        data: data,
        success: function(xhr) {
            success(xhr);
        },
        failure: function(xhr) {
            alert("Load article details failure!");
        },
        error: function (xhr, desc, err) {
            alert(desc);
        },
        dataType: "json"
    });

}